package com.example.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.core.Result;
import com.example.common.core.ResultGenerator;
import com.example.system.dao.ResourceDao;
import com.example.system.dao.RoleResourceDao;
import com.example.system.dto.ResourceParam;
import com.example.system.model.Resource;
import com.example.system.dto.ResourceResult;
import com.example.system.service.ResourceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Transactional
public class ResourceServiceImpl implements ResourceService {
    @javax.annotation.Resource
    private ResourceDao resourceDao;

    @javax.annotation.Resource
    private RoleResourceDao roleResourceDao;

    @Override
    public Result add(ResourceParam resourceParam) {
        Resource resource = BeanUtil.copyProperties(resourceParam, Resource.class);
        resourceDao.insertSelective(resource);
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public Result delete(Integer id) {
       return deleteBatch(Arrays.asList(id));
    }

    @Override
    public Result deleteBatch(List<Integer> ids) {
        resourceDao.deleteBatchByPrimaryKeys(ids);
        roleResourceDao.deleteBatchByResourceIds(ids);
        List<Resource> resources = resourceDao.selectByParentIds(ids);
        if (CollUtil.isNotEmpty(resources)) {
            List<Integer> idList = resources.stream().map(Resource::getId).collect(Collectors.toList());
            this.deleteBatch(idList);
        }
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public Result update(ResourceParam resourceParam) {
        Resource resource = BeanUtil.copyProperties(resourceParam, Resource.class);
        resourceDao.updateByPrimaryKeySelective(resource);
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public Result select(Integer id) {
        Resource resource = resourceDao.selectByPrimaryKey(id);
        if (ObjectUtil.isNull(resource)) {
            return ResultGenerator.genFailResult("资源不存在");
        }
        ResourceResult resourceResult = BeanUtil.copyProperties(resource, ResourceResult.class);
        return ResultGenerator.genSuccessResult(resourceResult);
    }

    @Override
    public Result selectList(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<ResourceResult> resourceResults = resourceDao.selectAll();
        PageInfo pageInfo = new PageInfo(resourceResults);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @Override
    public Result selectResourcesByRoleId(Integer id) {
        List<ResourceResult> resourceResults= resourceDao.selectResourcesByRoleId(id);
        return ResultGenerator.genSuccessResult(resourceResults);
    }
}
